<?php
namespace app\admin\controller;
use think\Db;
class Banner extends Common
{

    //首页轮播图展示
    public function index()
    {
        $banners=DB::name('banner')->order('displayorder desc')->select();
        foreach($banners as $key=>$value)
        {
            $banners[$key]['status']=$this->is_show($value['status']);
        }
        $this->assign('banners',$banners);
        return $this->fetch();
    }

    //添加/编辑banner
    public function postdata()
    {
        if(!request()->isPost()){
            $id=input('id');
            if(isset($id)){
                $banner_info=DB::name('banner')->where('id',$id)->find();
                $this->assign('banner_info',$banner_info);
            }
            return $this->fetch();
        }else{
            $postdata = input('post.');
            isset($postdata['pic']) &&$data['pic'] = $postdata['pic'];
            $data['status'] = $postdata['status'];
            $data['displayorder'] = $postdata['displayorder'];
            $data['link'] = $postdata['link'];
            if($postdata['id']!=0){
                DB::name('banner')->where('id',$postdata['id'])->update($data);
            }else{
                DB::name('banner')->insert($data);
            }
            //更新memcache
            $bannersbabyblue_banners=DB::name('banner')->where('status',1)->order('displayorder desc')->select();
            $this->mem->set('babyblue_banners',$bannersbabyblue_banners,0,0);
            $json = array();
            $json['status'] = '200';
            $json['data'] = $data;
            echo json_encode($json);
        }
    }

    public function del()
    {
        $id=input('id');
        if(!$id){
            $this->ajaxReturn('未检测到轮播图id');
        }
        $res=DB::name('banner')->where('id',$id)->delete();
        //更新memcache
        $bannersbabyblue_banners=DB::name('banner')->where('status',1)->order('displayorder desc')->select();
        $this->mem->set('babyblue_banners',$bannersbabyblue_banners,0,0);
        if($res){
            $this->ajaxJson('删除成功');
        }
    }
}
